package com.hqyj.system.controller;

import com.hqyj.system.model.Permission;
import com.hqyj.system.model.Role;
import com.hqyj.system.model.RolePermission;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import frame.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PermissionController
 * @Description
 * @Author zlf
 * @Date 2021/9/14 10:14
 * @Version 1.0
 **/
@Controller
@RequestMapping("/permissionController")
public class PermissionController extends BaseController {
    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission) {
        //1.接收数据;
        //3.调用业务;
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        for (Permission permission_db : permission_dbList) {
            System.out.println("====permissionController====List()======" + permission_db);
        }

        //返回数据
        model.addAttribute("permission_dbList", permission_dbList);
        //2.跳转页面;
        return "view/system/permission/permissionList";
    }

    @RequestMapping("/addUI.do")
    public String addUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission) {
        //1.接收数据;
        //3.调用业务;


        //2.跳转页面;
        //a.准备父ID
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);
        return "view/system/permission/addPermission";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String permissionIdSTR) {
        //1.接收数据;
        System.out.println("-----------1------" + permissionIdSTR);
        System.out.println("-----------2------" + permission);
        //3.调用业务;
        //字符类型转数字类型
        int permissionId = Integer.parseInt(permissionIdSTR);
        //维护关系
        permission.setpId(permissionId);
        //调用业务;
        int n = permissionService.addPermissionByPermission(permission);

        //2.跳转页面;
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);
        //2.跳转页面;
        return "view/system/permission/permissionList";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String permissionIdSTR) {
        //1.接收数据;
        System.out.println("-----delete-()-----1------" + permissionIdSTR);
        //字符类型转数字类型
        int permissionId = Integer.parseInt(permissionIdSTR);
        //维护关系
        permission.setpId(permissionId);

        //3.调用业务;
        int n = permissionService.deletePermissionByPermissionId(permissionId);

        //2.跳转页面;
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);
        //2.跳转页面;
        return "view/system/permission/permissionList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String permissionIdSTR) {
        //1.接收数据;

        //字符类型转数字类型
        int permissionId = Integer.parseInt(permissionIdSTR);
        //维护关系
        permission.setpId(permissionId);

        //3.调用业务;
        //a.修改数据
        Permission permission_db = permissionService.queryPermissionByPermissionId(permissionId);
        model.addAttribute("permission_db", permission_db);


        //b.父级数据
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);

        //b.父级数据
        //维护关系
        permissionId = permission_db.getpId();
        Permission permission_db_FJ = permissionService.query_FJ_PermissionByPermissionId(permissionId);
        model.addAttribute("permission_db_FJ", permission_db_FJ);


        //2.跳转页面;
        return "view/system/permission/updatePermission";
    }


    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String permissionIdSTR, String permissionIdSTR_FJ) {

        //1.接收数据;
        int permissionId = Integer.parseInt(permissionIdSTR);
        permission.setPermissionId(permissionId);
        int pId = Integer.parseInt(permissionIdSTR_FJ);
        permission.setpId(pId);
        System.out.println("--------update()-----------" + permission);

        //3.调用业务;
        int n = permissionService.updatePermissionByPermission(permission);

        //2.跳转页面;
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);
        return "view/system/permission/permissionList";
    }

    @RequestMapping("/FPPermissionUI.do")
    public String FPPermissionUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Permission permission, String permissionIdSTR, String permissionIdSTR_FJ, String roleIdStr, Role role) {
        //1.接收数据;
        System.out.println("------------FPPermissionUI--------------" + roleIdStr);
        //字符类型转成数字类型
        int roleId = Integer.parseInt(roleIdStr);

        //3.调用业务;
        //a.分配权限角色
        Role role_db = roleService.queryRoleByRoleId(roleId);
        model.addAttribute("role_db", role_db);

        //b.分配权限对象集合
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);


        //d.选择权限数据
        List<Permission> permission_xz_dbList = permissionService.query_xz_permissionByRoleId(roleId);
        //两个对象页面对比不行
        //model.addAttribute("permission_xz_dbList", permission_xz_dbList);

        List<Integer> list = new ArrayList<Integer>();
        for (Permission permission_xz_db : permission_xz_dbList) {
            list.add(permission_xz_db.getPermissionId());
        }
        model.addAttribute("ids", list);


        //2.跳转页面;
        return "view/system/permission/FPPermissionUI";
    }

    @RequestMapping("/FPPermission.do")
    public String FPPermission(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, RolePermission rolePermission, Permission permission, String permissionIdSTR, String permissionIdSTR_FJ, String roleIdStr, Role role, String[] permissionIdStrs) {
        //1.接收数据;
        //字符类型转成数字类型
        int roleId = Integer.parseInt(roleIdStr);
        //先删除
        int m = rolePermissionService.deleteRolePermissionByRoleId(roleId);
        for (String permissionIdStr : permissionIdStrs) {
            //字符类型转成数字类型
            int permissionId = Integer.parseInt(permissionIdStr);
            //3.调用业务;
            //封装
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            //业务
            int n = rolePermissionService.addRolePermissionByRolePermission(rolePermission);
        }

        //2.跳转页面;
        //a.分配权限角色
        Role role_db = roleService.queryRoleByRoleId(roleId);
        model.addAttribute("role_db", role_db);

        //b.分配权限对象集合
        List<Permission> permission_dbList = permissionService.queryAllPermission();
        model.addAttribute("permission_dbList", permission_dbList);

        //c.友情提示
        model.addAttribute("message", "分配权限成功，继续分配吗？");

        //d.选择权限数据
        List<Permission> permission_xz_dbList = permissionService.query_xz_permissionByRoleId(roleId);
        //两个对象页面对比不行
        //model.addAttribute("permission_xz_dbList", permission_xz_dbList);

        List<Integer> list = new ArrayList<Integer>();
        for (Permission permission_xz_db : permission_xz_dbList) {
            list.add(permission_xz_db.getPermissionId());
        }
        model.addAttribute("ids", list);


        return "view/system/permission/FPPermissionUI";
    }


}
